home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* sprite.c
- * This program demonstrates GL_SGIX_sprite.
- *
- * <s> key - toggle GL_SGIX_sprite on/off
- * Escape key - exit the program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid toggleSpriteEnable( GLvoid );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLint spriteMode;
-
- static GLboolean spriteEnable = GL_FALSE;
- static GLboolean spriteSupported = GL_TRUE;
-
- static GLfloat angle = 80.0;
-
- static GLfloat spriteAxis[] = {.0, .0, 1.0, 1.0 };
- static GLfloat spriteTrans[] = {.0, .0, .0, 1.0 };
-
- typedef struct VtxRec {
- GLfloat crd[4]; /* coordinate */
- GLfloat col[4]; /* color */
- GLfloat nrm[3]; /* normal */
- GLfloat tex[4]; /* texture coordinate */
- } Vtx;
-
- typedef struct QdRec {
- Vtx v0, v1, v2, v3;
- } Qd;
-
- static Qd qd = {
- { {0.1, 0.1, 0.0, 1.0}, {1.0, 1.0, 1.0, 1.0},
- {1.0, 1.0, 1.0}, {0.1, 0.1, .0, 1.0} },
- { {0.1, 0.2, .0, 1.0}, {1.0, .0, .0, .0},
- {1.0, 1.0, 1.0}, {0.1, 0.9, .0, 1.0} },
- { {0.2, 0.2, .0, 1.0}, {.0, 1.0, .0, .0},
- {1.0, 1.0, 1.0}, {0.9, 0.9, .0, 1.0} },
- { {0.2, 0.1, .0, 1.0}, {.0, .0, 1.0, .0},
- {1.0, 1.0, 1.0}, {0.9, 0.1, .0, 1.0} },
- };
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width/4, height/4 );
- glutInitWindowSize( width/2, height/2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout,"\n%s - demonstrates sprites\n\n"
- "<s> key - toggle sprites\n"
- "Escape key - exit the program\n\n",
- progname
- );
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- glClearColor(0, 0, 0, 0);
- glClear(GL_COLOR_BUFFER_BIT);
-
- if (!glutExtensionSupported("GL_SGIX_sprite")) {
- spriteSupported = GL_FALSE;
- fprintf( stderr,
- "GL_SGIX_sprite not supported on this machine\n");
- }
- }
-
- void toggleSpriteEnable( GLvoid )
- {
- #ifdef GL_SGIX_sprite
- if (spriteSupported) {
- spriteEnable = !spriteEnable;
- printf("Sprites are %s\n", (spriteEnable? "Enabled":"Disabled"));
- } else {
- printf("GL_SGIX_sprite not supported\n");
- }
- #else
- printf("Sprites are not supported on this client\n");
- #endif
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 's': /* toggle sprite enable */
- toggleSpriteEnable();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- }
-
- static void
- drawObject(void)
- {
- glBegin(GL_QUADS);
- glColor4f(qd.v0.col[0], qd.v0.col[1], qd.v0.col[2], qd.v0.col[3]);
- glTexCoord4f(qd.v0.tex[0], qd.v0.tex[1], qd.v0.tex[2], qd.v0.tex[3]);
- glNormal3f(qd.v0.nrm[0], qd.v0.nrm[1], qd.v0.nrm[2]);
- glVertex4f(qd.v0.crd[0], qd.v0.crd[1], qd.v0.crd[2], qd.v0.crd[3]);
-
- glColor4f(qd.v1.col[0], qd.v1.col[1], qd.v1.col[2], qd.v1.col[3]);
- glTexCoord4f(qd.v1.tex[0], qd.v1.tex[1], qd.v1.tex[2], qd.v1.tex[3]);
- glNormal3f(qd.v1.nrm[0], qd.v1.nrm[1], qd.v1.nrm[2]);
- glVertex4f(qd.v1.crd[0], qd.v1.crd[1], qd.v1.crd[2], qd.v1.crd[3]);
-
- glColor4f(qd.v2.col[0], qd.v2.col[1], qd.v2.col[2], qd.v2.col[3]);
- glTexCoord4f(qd.v2.tex[0], qd.v2.tex[1], qd.v2.tex[2], qd.v2.tex[3]);
- glNormal3f(qd.v2.nrm[0], qd.v2.nrm[1], qd.v2.nrm[2]);
- glVertex4f(qd.v2.crd[0], qd.v2.crd[1], qd.v2.crd[2], qd.v2.crd[3]);
-
- glColor4f(qd.v3.col[0], qd.v3.col[1], qd.v3.col[2], qd.v3.col[3]);
- glTexCoord4f(qd.v3.tex[0], qd.v3.tex[1], qd.v3.tex[2], qd.v3.tex[3]);
- glNormal3f(qd.v3.nrm[0], qd.v3.nrm[1], qd.v3.nrm[2]);
- glVertex4f(qd.v3.crd[0], qd.v3.crd[1], qd.v3.crd[2], qd.v3.crd[3]);
- glEnd();
-
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- int i, slices = 8;
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- drawObject();
-
- #ifdef GL_SGIX_sprite
- if (spriteEnable) {
- glEnable(GL_SPRITE_SGIX);
- glSpriteParameteriSGIX(GL_SPRITE_MODE_SGIX, GL_SPRITE_AXIAL_SGIX);
- }
- #endif
-
- /* axial mode (clipped geometry) */
- glPushMatrix();
- glTranslatef(.15, .0, .0);
-
- #ifdef GL_SGIX_sprite
- if (spriteEnable) {
- spriteAxis[0] = .2; spriteAxis[1] = .2; spriteAxis[2] = 1.0;
- glSpriteParameterfvSGIX(GL_SPRITE_AXIS_SGIX, spriteAxis);
-
- spriteTrans[0] = .2; spriteTrans[1] = .0; spriteTrans[2] = .0;
- glSpriteParameterfvSGIX(GL_SPRITE_TRANSLATION_SGIX, spriteTrans);
- }
- #endif
-
- drawObject();
- glPopMatrix();
-
- /* axial mode (non-clipped geometry) */
- glPushMatrix();
- glTranslatef(.3, .1, .0);
-
- #ifdef GL_SGIX_sprite
- if (spriteEnable) {
- spriteAxis[0] = .2; spriteAxis[1] = .2; spriteAxis[2] = 0.5;
- glSpriteParameterfvSGIX(GL_SPRITE_AXIS_SGIX, spriteAxis);
-
- spriteTrans[0] = .2; spriteTrans[1] = .2; spriteTrans[2] = .0;
- glSpriteParameterfvSGIX(GL_SPRITE_TRANSLATION_SGIX, spriteTrans);
- }
- #endif
-
- drawObject();
- glPopMatrix();
-
- #ifdef GL_SGIX_sprite
- if (spriteEnable) {
- /* object mode */
- glSpriteParameteriSGIX(GL_SPRITE_MODE_SGIX, GL_SPRITE_OBJECT_ALIGNED_SGIX);
- }
- #endif
- glPushMatrix();
- glTranslatef(.0, .12, .0);
-
- #ifdef GL_SGIX_sprite
- if (spriteEnable) {
- spriteAxis[0] = .8; spriteAxis[1] = .5; spriteAxis[2] = 1.0;
- glSpriteParameterfvSGIX(GL_SPRITE_AXIS_SGIX, spriteAxis);
-
- spriteTrans[0] = .0; spriteTrans[1] = .3; spriteTrans[2] = .0;
- glSpriteParameterfvSGIX(GL_SPRITE_TRANSLATION_SGIX, spriteTrans);
- }
- #endif
-
- drawObject();
- glPopMatrix();
-
-
- #ifdef GL_SGIX_sprite
- if (spriteEnable) {
- /* eye mode */
- glSpriteParameteriSGIX(GL_SPRITE_MODE_SGIX, GL_SPRITE_EYE_ALIGNED_SGIX);
- }
- #endif
- glPushMatrix();
- glTranslatef(.15, .25, .0);
-
- #ifdef GL_SGIX_sprite
- if (spriteEnable) {
- spriteAxis[0] = .0; spriteAxis[1] = 1.0; spriteAxis[2] = 1.0;
- glSpriteParameterfvSGIX(GL_SPRITE_AXIS_SGIX, spriteAxis);
-
- spriteTrans[0] = .2; spriteTrans[1] = .2; spriteTrans[2] = .0;
- glSpriteParameterfvSGIX(GL_SPRITE_TRANSLATION_SGIX, spriteTrans);
- }
- #endif
-
- drawObject();
- glPopMatrix();
-
- #ifdef GL_SGIX_sprite
- if (spriteEnable)
- glDisable(GL_SPRITE_SGIX);
- #endif
-
- glutSwapBuffers();
-
- checkError("drawScene");
- }
-